#!/usr/bin/env groovy

// def label = "mypod-${UUID.randomUUID().toString()}"

podTemplate(cloud: 'kubernetes', inheritFrom: 'default', containers: [
            containerTemplate(name: 'maven', image: 'harbor.k2software.com.cn/build/maven:3.6.0-jdk-8', command: 'cat', ttyEnabled: true),
            containerTemplate(name: 'sonar', image: 'harbor.k2software.com.cn/build/sonar-scanner-cli:4.3', command: 'cat', ttyEnabled: true),

            ],
            volumes: [
                    hostPathVolume(hostPath: '/root/.kube', mountPath: '/root/.kube'),
                    hostPathVolume(hostPath: '/root/.m2', mountPath: '/root/.m2')
            ],
            annotations: [
                    podAnnotation(key: "sidecar.istio.io/inject", value: "false")
            ],
            //nodeSelector: "devops=build",
            //nodeUsageMode : "EXCLUSIVE"

) {

    node(POD_LABEL) {
        container('maven') {

            stage('checkout') {
                checkout scm

                sh 'printenv'
            }

            stage('pacakge') {
              ansiColor('xterm') {
                sh 'mvn clean package -Dmaven.test.skip=true -s ./settings.xml'
              }
            }
        }

        container('sonar') {
           stage ('sonar analyze') {
                def sonarHost = "http://sonar.poc.paas.com"
                def sonarToken = "235ec10b476da2cf5f5b68c20707d85b48080636"
                def sonarProjectName = "springboot-k8s-example"
                def sonarProjectVersion = "1.0.0"
                def sonarSources = "src"
                def sonarBinaries = "target/classes"

             ansiColor('xterm') {
                
                sh """
                sonar-scanner -Dsonar.host.url=$sonarHost -Dsonar.login=$sonarToken \
                -Dsonar.projectKey=$sonarProjectName -Dsonar.projectName=$sonarProjectName \
                -Dsonar.projectVersion=$sonarProjectVersion -Dsonar.sources=$sonarSources \
                -Dsonar.java.binaries=$sonarBinaries -Dsonar.language=java \
                -Dsonar.sourceEncoding=UTF-8
                """
            }
           }
        }

  }
}

// vim: ft=groovy
